package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Admin;
import com.atguigu.entity.HouseImage;
import com.atguigu.service.AdminService;
import com.atguigu.service.RoleService;
import com.atguigu.util.QiniuUtils;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.parameters.P;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * projectName: shf
 *
 * @author: cbc
 * time: 2022/6/7 15:36 周二
 * description: 用户
 */
@Controller
@RequestMapping("/admin")
public class AdminController extends BaseController {
    @Reference
    private AdminService adminService;

    @Reference
    private RoleService roleService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    private final static String PAGE_INDEX = "admin/index";
    private final static String PAGE_CREATE = "admin/create";
    private final static String PAGE_EDIT = "admin/edit";
    private final static String PAGE_SUCCESS = "common/successPage";
    private final static String PAGE_UPLOED_SHOW = "admin/upload";
    private final static String PAGE_ASSGIN_SHOW = "admin/assignShow";
    private final static String PAGE_PERSONAL = "admin/personal";

    /**
     * 列表
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping
    public String index(ModelMap model, HttpServletRequest request) {
        Map<String, Object> filters = getFilters(request);
        PageInfo<Admin> page = adminService.findPage(filters);

        model.addAttribute("page", page);
        model.addAttribute("filters", filters);
        return PAGE_INDEX;
    }

    /**
     * 进入新增页面
     *
     * @return
     */
    @GetMapping("/create")
    public String create() {
        return PAGE_CREATE;
    }

    /**
     * 保存新增
     *
     * @param admin
     * @param
     * @return
     */
    @PostMapping("/save")
    public String save(Admin admin) {
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        admin.setPassword(encoder.encode(admin.getPassword()));
        //设置默认头像
        admin.setHeadUrl("http://47.93.148.192:8080/group1/M00/03/F0/rBHu8mHqbpSAU0jVAAAgiJmKg0o148.jpg");
        adminService.insert(admin);

        return PAGE_SUCCESS;
    }

    /**
     * 进入编辑页面
     *
     * @param model
     * @param id
     * @return
     */
    @GetMapping("/edit/{id}")
    public String edit(ModelMap model, @PathVariable Long id) {
        Admin admin = adminService.getById(id);
        model.addAttribute("admin", admin);
        return PAGE_EDIT;
    }

    /**
     * 保存更新
     *
     * @param admin
     * @return
     */
    @PostMapping(value = "/update")
    public String update(Admin admin) {

        adminService.update(admin);

        return PAGE_SUCCESS;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    public String delete(@PathVariable Long id) {
        adminService.delete(id);
        return "redirect:/admin";
    }

    /**
     * 上传页面
     *
     * @param modelMap
     * @param id
     * @return
     */
    @GetMapping("/uploadShow/{id}")
    public String uploadShow(ModelMap modelMap, @PathVariable Long id) {
        modelMap.addAttribute("id", id);
        return PAGE_UPLOED_SHOW;
    }

    /**
     * 上传方法
     *
     * @param id
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload/{id}")
    public String upload(
            @PathVariable Long id,
            @RequestParam(value = "file") MultipartFile file,
            HttpServletRequest request) throws IOException {
        try {
            String newFileName = UUID.randomUUID().toString();
            //上传图片
            QiniuUtils.upload2Qiniu(file.getBytes(), newFileName);
            String url = "http://rde8ksxen.hn-bkt.clouddn.com/" + newFileName;
            Admin admin = new Admin();
            admin.setId(id);
            admin.setHeadUrl(url);
            adminService.update(admin);
            return PAGE_SUCCESS;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 进入分配角色页面
     *
     * @param modelMap
     * @param adminId
     * @return
     */
    @RequestMapping("/assignShow/{adminId}")
    public String assignShow(ModelMap modelMap, @PathVariable Long adminId) {
        //根据用户id，查询用户角色
        Map<String, Object> roleMap = roleService.findRoleByAdminId(adminId);
        modelMap.addAllAttributes(roleMap);
        modelMap.addAttribute("adminId", adminId);
        return PAGE_ASSGIN_SHOW;
    }

    @PostMapping("/assignRole")
    public String assignRloe(Long adminId, Long[] roleIds) {
        roleService.saveUserRoleRealtionShip(adminId, roleIds);
        return PAGE_SUCCESS;
    }

    @RequestMapping("/personal/{id}")
    public String personal(ModelMap modelMap, @PathVariable Long id) {
        Admin admin = adminService.getById(id);
        modelMap.addAttribute("admin", admin);
        return PAGE_PERSONAL;
    }
}
